package com.tuniu.agents.tools.flights.international.oneway;

import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.tuniu.agents.advisor.ChatEnvironmentMemory;
import com.tuniu.agents.tools.flights.FlightProperties;
import com.tuniu.agents.util.HttpManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;

public class IntlFlightInformationQueryTool implements BiFunction<IntlFlightInformationQueryTool.Request, ToolContext,  IntlFlightInformationQueryTool.Response> {

    public static final String TOOLNAME = "IntlFlightInformationQueryTool";

    private static final Logger logger = LoggerFactory.getLogger(IntlFlightInformationQueryTool.class);


    private final String flightApiUrl;

    @Autowired(required = false)
    private ChatEnvironmentMemory chatEnvironmentMemory;

    @Autowired
    private HttpManager httpManager;

    public IntlFlightInformationQueryTool(FlightProperties exampleProperties) {
        this.flightApiUrl = exampleProperties.getIntlFlightInformationQueryToolUrl();
    }

    @Override
    public Response apply(Request request, ToolContext context) {
        Map<String,Object> map = context.getContext();
        logger.info("IntFlightInformationQueryTool 请求入参:{}", request);
        Request requestNew = new Request(request.flightNo, (String)chatEnvironmentMemory.get((String)map.get("conversation_id")).get("queryId"));
        logger.info("flightApiUrl:{}", flightApiUrl);
        return httpManager.post(flightApiUrl, requestNew, Response.class);
    }

    @JsonClassDescription("国际单程机票航班详情查询入参")
    public record Request(
            @JsonProperty(value = "flightNo") @JsonPropertyDescription("指定的单个国际航班号如MU1234，或多个国际航班号拼接MU1234-MU2233") String flightNo,
            @JsonProperty(value = "queryId") @JsonPropertyDescription("列表返回的查询快照ID") String queryId) {
    }

    @JsonClassDescription("国际单程机票航班详情查询返参")
    public record Response(
            @JsonProperty(required = true,
                    value = "successCode") @JsonPropertyDescription("响应码") boolean successCode,
            @JsonProperty(required = true,
                    value = "success") @JsonPropertyDescription("成功标识，成功：\"查询成功！\"，失败：\"查询失败！\"") String success,
            @JsonProperty(required = true,
                    value = "flightInfo") @JsonPropertyDescription("航程信息") List<FlightInfo> flightInfo,
            @JsonProperty(required = true,
                    value = "cabinInfo") @JsonPropertyDescription("舱位信息") List<CabinInfo> cabinInfo) {

    }

    @JsonClassDescription("航程信息")
    public record FlightInfo(
            @JsonProperty(value = "departureAirport") @JsonPropertyDescription("出发机场") String departureAirport,
            @JsonProperty(value = "departureTerminal") @JsonPropertyDescription("出发航站楼") String departureTerminal,
            @JsonProperty(value = "arrivalAirport") @JsonPropertyDescription("到达机场") String arrivalAirport,
            @JsonProperty(value = "arrivalTerminal") @JsonPropertyDescription("到达航站楼") String arrivalTerminal,
            @JsonProperty(value = "flightNumber") @JsonPropertyDescription("航班号") String flightNumber,
            @JsonProperty(value = "airComImageUrl") @JsonPropertyDescription("航司图标url") List<String> airComImageUrl,
            @JsonProperty(value = "crafType") @JsonPropertyDescription("机型") String crafType,
            @JsonProperty(value = "type") @JsonPropertyDescription("航班类型：直飞，中转") String type,
            @JsonProperty(value = "stopPoint") @JsonPropertyDescription("经停点") String stopPoint,
            @JsonProperty(value = "stopTime") @JsonPropertyDescription("经停时长") String stopTime,
            @JsonProperty(value = "departureTime") @JsonPropertyDescription("出发时间") String departureTime,
            @JsonProperty(value = "arrivalTime") @JsonPropertyDescription("到达时间") String arrivalTime,
            @JsonProperty(value = "punctualityRate") @JsonPropertyDescription("准点率") String punctualityRate,
            @JsonProperty(value = "totalDuration") @JsonPropertyDescription("总时长") String totalDuration,
            @JsonProperty(value = "planModel") @JsonPropertyDescription("机型大小，1 大，2 中，3 小") String planModel,
            @JsonProperty(value = "shareFlightNo") @JsonPropertyDescription("实际承运航班号") String shareFlightNo,
            @JsonProperty(value = "cabinClass") @JsonPropertyDescription("舱等") String cabinClass,
            @JsonProperty(value = "mealCode") @JsonPropertyDescription("餐食") String mealCode,
            @JsonProperty(value = "transferTime") @JsonPropertyDescription("中转时间") String transferTime,
            @JsonProperty(value = "transferPoint") @JsonPropertyDescription("中转地") String transferPoint,
            @JsonProperty(value = "visaTips") @JsonPropertyDescription("过境签") String visaTips,
            @JsonProperty(value = "hanging") @JsonPropertyDescription("托运行李") String hanging,
            @JsonProperty(value = "airlineCompany") @JsonPropertyDescription("航司") String airlineCompany) {
    }

    @JsonClassDescription("舱位信息")
    public record CabinInfo(
            @JsonProperty(value = "aiIntelCabinInfos") @JsonPropertyDescription("往返仓位信息") List<CabinDetailInfo> aiIntelCabinInfos,
            @JsonProperty(value = "price") @JsonPropertyDescription("带税价格") String price,
            @JsonProperty(value = "sourceId") @JsonPropertyDescription("资源ID：vendorID + expandPriceID") String sourceId) {
    }

    @JsonClassDescription("往返舱位信息")
    public record CabinDetailInfo(
            @JsonProperty(value = "baggageInfo") @JsonPropertyDescription("行李信息") String baggageInfo,
            @JsonProperty(value = "reimbursementProof") @JsonPropertyDescription("报销凭证") String reimbursementProof,
            @JsonProperty(value = "remainingSeats") @JsonPropertyDescription("余位") String remainingSeats,
            @JsonProperty(value = "saleControl") @JsonPropertyDescription("销售控制") List<String> saleControl,
            @JsonProperty(value = "cabinCode") @JsonPropertyDescription("仓位") String cabinCode) {
    }

}
